// pages/index/parking/parking.js
let request = require("../../../utils/request.js"); //封装的request请求

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    selectedStopingCar: Object,
    stopingCarList: Array
  },

  /**
   * 组件的初始数据
   */
  data: {
    parkTicketDialog: false,
    parkTicketList: [],
    selectedParkTicket: null
  },

  lifetimes: {
    attached: function() {
      console.log('在组件实例进入页面节点树时执行')
      this.getCurrOrder(true)
    },
    detached: function() {
      console.log('在组件实例被从页面节点树移除时执行')
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
      /**
     * 获取车主实时订单
     * @param {boolean} isOut 是否点击外部离场按钮
     */
    getCurrOrder(isOut) {
      let paramsList = []
      paramsList.push({
        orderCode: this.data.selectedStopingCar.orderCode,
        parkId: this.data.selectedStopingCar.parkId,
        carNumber: this.data.selectedStopingCar.carNumber
      })
      let params = { list: paramsList }
      request.http(
        '/order/orderInfo/queryCurrOrder', 'GET', params, 1
      ).then(res => {
        console.log('车主实时订单：','响应时间1111', res.data.data)
        if (res.data.code == 200) {
          let data = res.data.data
          //  客户停车券
          this.getParkTicket(this.data.selectedStopingCar.parkId, data[0].duration,data[0].orderCode)
          let time = ''
          for (var i = 0; i < data.length; i++) {
            time = Math.floor(data[i].duration / 60) > 0 ? Math.floor(data[i].duration / 60) + '小时' + data[i].duration % 60 + '分钟' : data[i].duration % 60 + '分钟'
            data[i].duration = time
          }
          this.setData({
            selectedStopingCar: res.data.data[0],
            stopCarInfoList: res.data.data
          })
          // if (isOut) {
          //   this.setData({ stopCarOutDialog: true })
          // } else {
          //   this.triggerEvent('getStopingCar', false)
          // }
          // payCurrOrderFlag = false
        }
        // currOrderFlag = false
      }).catch(err => {
        console.log(err)
        this.setData({
          // stopCarDialog: true,
          // stopCarOutDialog: false,
          stop_car_tips: err.data.desc
        })
        // currOrderFlag = false
      })
    },
    /**
     * 客户停车券
     */
    getParkTicket(parkId, duration,orderCode){
      let params = { parkId: parkId, duration: duration,orderCode:orderCode }
      request.http(
        '/customer/ticket/get-can-use-park-ticket', 'GET', params, 1
      ).then(res => {
        console.log('客户停车券：', res)
        if (res.data.code == 200) {
          let parkTicketList = res.data.data
          // parkTicketList.sort(function(a, b){
          //   return b.parkingDuration - a.parkingDuration  //  降序
          // })
          parkTicketList.forEach((item)=> {
            item.map.forEach((res)=>{
              res.useEndTime = res.useEndTime.substring(0, 10)
            })
          })
          if(parkTicketList.length > 0){
            parkTicketList[0].checked = true
          }

          this.setData({ parkTicketList: parkTicketList, selectedParkTicket: parkTicketList[0] })

        }
      })
    },
        /**
     * 打开选择停车券弹框
     */
    showSelectParkTicketDialog() {
      this.setData({ parkTicketDialog: true })
    },
    /**
     * 选择停车券
     */
    selectParkTicket(e) {
      let parkTicketList = this.data.parkTicketList
      parkTicketList.forEach((parkTicket)=> {
        parkTicket.checked = false
      })
      parkTicketList[e.currentTarget.dataset.parkTicketIndex].checked = true

      this.setData({ parkTicketList: parkTicketList, selectedParkTicket:  parkTicketList[e.currentTarget.dataset.parkTicketIndex] })
    },
    /**
     * 选择停放车辆
     */
    stopCarChange(e) {
      console.log('切换车牌：', e)
      let stopingCarList = this.data.stopingCarList
      for (var i = 0; i < stopingCarList.length; i++) {
        stopingCarList[i].checked = false
        if (stopingCarList[i].carNumber == e.detail.value) {
          stopingCarList[i].checked = true
          this.setData({ selectedStopingCar: stopingCarList[i] })
        }
      }

      this.setData({ stopingCarList: stopingCarList })
      //  获取车主实时订单
      this.getCurrOrder()
    },
    // 预支付
    prePay() {
      // if (payCurrOrderFlag) { return }
      if (!this.data.selectedStopingCar.orderCode) {
        return
      }
      let params = {
        orderCode: this.data.selectedStopingCar.orderCode
      }
      if(this.data.selectedParkTicket){
        console.log('11');
        params.ticketIdList = [];
        // params.custTicketId = this.data.selectedParkTicket.id
        this.data.selectedParkTicket.map.map((res,index)=>{
          params.ticketIdList.push(res.id)
          console.log(res);
        })
      }
      console.log(typeof params.ticketIdList)
      console.log(params.ticketIdList)
      console.log(typeof params)
      wx.showLoading({ title: '正在支付' })
      request.http(
        '/order/orderInfo/prePayCurrOrder', 'POST', params, 1, true
      ).then(res => {
        if (res.data.code == 200) {
          this.getPaySuccess(res.data.data)
        }
      })
    },
    /**
     * 根据交易流水号查询订单是否支付成功
     */
    getPaySuccess(data) {
      let _this = this
      request.http(
        '/order/orderInfo/getOrderTradeRecordByTradeNo', 'POST', { orderCode: data.order_id, tradeNo: data.trade_no }
      ).then(res => {
        if (res.data.code == 200) {
          wx.showToast({
            title: '请在15分钟内离场，超时将重新计费',
            duration: 3000,
            icon: 'none',
            success(res) {
              // _this.triggerEvent('getStopingCar')
              let selectedStopingCar = _this.data.selectedStopingCar
              selectedStopingCar.money = 0
              _this.setData({
                selectedStopingCar: selectedStopingCar,
                stop_car_tips: '请在15分钟内离场，超时将重新计费',
              })
            }
          })
        }
      })
    },
    /**
     * 关闭停车信息弹窗
     */
    hiddenStopCarOutDialog() {
      this.triggerEvent('hiddenStopCarOutDialog')
    },
    /**
     * 关闭弹窗
     */
    closeDialog() {
      this.setData({ parkTicketDialog: false })
    }
  }
})
